<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="客户名称" name="customName">
						<a-input v-model:value="searchFormState.customName" placeholder="请输入客户名称"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="客户类型" name="customCustomType">
						<xn-page-select
							ref="xncustomCustomTypePageSelectRef"
							v-model:value="searchFormState.customCustomType"
							placeholder="请选择客户类型"
							allow-clear
							show-search
							:page-function="customCustomTypeSelector"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="客户电话" name="customTel">
						<a-input v-model:value="searchFormState.customTel" placeholder="请输入客户电话"/>
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="客户地址" name="customAddress">
						<a-input v-model:value="searchFormState.customAddress" placeholder="请输入客户地址"/>
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="所属门店" name="customStore">
						<xn-page-select
							ref="xncustomStorePageSelectRef"
							v-model:value="searchFormState.customStore"
							placeholder="请选择所属门店"
							allow-clear
							show-search
							:page-function="customStoreSelector"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="客户标签" name="customTag">
						<xn-page-select
							ref="xncustomTagPageSelectRef"
							v-model:value="searchFormState.customTag"
							placeholder="请选择客户标签"
							allow-clear
							show-search
							:page-function="customTagSelector"
						/>
					</a-form-item>
				</a-col>

				<a-col :span="6" v-show="advanced">
					<a-form-item label="默认发货方式" name="customDelivery">
						<xn-page-select
							ref="xncustomDeliveryPageSelectRef"
							v-model:value="searchFormState.customDelivery"
							placeholder="请选择默认发货方式"
							allow-clear
							show-search
							:page-function="customDeliverySelector"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
					</a>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('supplierCustomCustomAdd')">
						<template #icon>
							<plus-outlined/>
						</template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('supplierCustomCustomBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchSupplierCustom"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'objectStatus'">
					<a-tag v-if="record.objectStatus === '0'" color="red">
						停用
					</a-tag>
					<a-tag v-if="record.objectStatus === '1'" color="green">
						正常
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'objectAllowtovisitmail'">
					<a-tag v-if="record.objectAllowtovisitmail === '0'" color="red">
						禁止
					</a-tag>
					<a-tag v-if="record.objectAllowtovisitmail === '1'" color="green">
						允许
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'customAvatar'">
					<a-image :src="record.customAvatar" style="width: 30px; height: 30px;"/>
				</template>
				<template v-if="column.dataIndex === 'customAttachment'">
					<a-image :src="record.customAttachment" style="width: 30px; height: 30px;"/>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('supplierCustomCustomEdit')">编辑</a>
						<a-divider type="vertical"
								   v-if="hasPerm(['supplierCustomCustomEdit', 'supplierCustomCustomDelete'], 'and')"/>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteSupplierCustom(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('supplierCustomCustomDelete')">
								删除
							</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()"/>
</template>

<script setup name="supplierCustomCustom">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import Form from './form.vue'
import supplierObjectsApi from "@/api/supplier/custom/supplierObjectsApi";
import supplierCustomTypeApi from "@/api/supplier/custom/supplierCustomTypeApi";
import supplierStoresApi from "@/api/supplier/base/supplierStoresApi";
import supplierCustomTagApi from "@/api/supplier/custom/supplierCustomTagApi";
import supplierDeliveryApi from "@/api/supplier/base/supplierDeliveryApi";

const searchFormState = ref({})
const searchFormRef = ref()
const tableRef = ref()
const formRef = ref()
const toolConfig = {refresh: true, height: true, columnSetting: true, striped: false}
// 查询区域显示更多控制
const advanced = ref(false)
const toggleAdvanced = () => {
	advanced.value = !advanced.value
}
const columns = [
	{
		title: '客户名称',
		dataIndex: 'objectName'
	},
	{
		title: '客户类型',
		dataIndex: 'objectGradeIdName'
	},
	{
		title: '客户电话',
		dataIndex: 'objectTel'
	},
	{
		title: '客户地址',
		dataIndex: 'objectAddress',
		ellipsis: true
	},
	{
		title: '负责人',
		dataIndex: 'objectPersonName'
	},
	{
		title: '所属门店',
		dataIndex: 'objectStoreIdName'
	},
	{
		title: '客户标签',
		dataIndex: 'objectTagName',
		checked: false
	},
	{
		title: '客户状态',
		dataIndex: 'objectStatus'
	},
	{
		title: '联系人',
		dataIndex: 'objectContact'
	},
	{
		title: '邮箱',
		dataIndex: 'objectEmail',
		checked: false
	},
	{
		title: '开户行',
		dataIndex: 'objectBank',
		checked: false
	},
	{
		title: '银行账户',
		dataIndex: 'objectBankCode',
		checked: false
	},
	{
		title: '默认折扣',
		dataIndex: 'objectDiscount',
		checked: false
	},
	{
		title: '客户积分',
		dataIndex: 'objectScore'
	},
	{
		title: '累计积分',
		dataIndex: 'objectScoreTotal'
	},
	{
		title: '推荐人',
		dataIndex: 'objectReference',
		checked: false
	},
	{
		title: '允许访问商城',
		dataIndex: 'objectAllowtovisitmail',
		checked: false
	},
	{
		title: '默认发货方式',
		dataIndex: 'objectDeliveryName',
		checked: false
	},
	{
		title: '备注',
		dataIndex: 'objectRemark',
		checked: false
	},
	{
		title: '应收账款',
		dataIndex: 'objectAccountsPayable'
	}
]
// 操作栏通过权限判断是否显示
if (hasPerm(['supplierCustomCustomEdit', 'supplierCustomCustomDelete'])) {
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: 150
	})
}
const selectedRowKeys = ref([])
// 列表选择配置
const options = {
	// columns数字类型字段加入 needTotal: true 可以勾选自动算账
	alert: {
		show: true,
		clear: () => {
			selectedRowKeys.value = ref([])
		}
	},
	rowSelection: {
		onChange: (selectedRowKey, selectedRows) => {
			selectedRowKeys.value = selectedRowKey
		}
	}
}
const loadData = (parameter) => {
	const searchFormParam = cloneDeep(searchFormState.value)
	searchFormParam.objectType = "1" //客户
	return supplierObjectsApi.supplierObjectsPage(Object.assign(parameter, searchFormParam)).then((data) => {
		return data
	})
}
// 重置
const reset = () => {
	searchFormRef.value.resetFields()
	tableRef.value.refresh(true)
}
// 删除
const deleteSupplierCustom = (record) => {
	let params = [
		{
			id: record.id
		}
	]
	supplierObjectsApi.supplierObjectsDelete(params).then(() => {
		tableRef.value.refresh(true)
	})
}
// 批量删除
const deleteBatchSupplierCustom = (params) => {
	supplierObjectsApi.supplierObjectsDelete(params).then(() => {
		tableRef.value.clearRefreshSelected()
	})
}
const xncustomCustomTypePageSelectRef = ref();
const customCustomTypeSelector = (param) => {
	return supplierCustomTypeApi.supplierCustomTypePage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.customTypeName,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
nextTick(() => {
	xncustomCustomTypePageSelectRef.value.onPage({current: 1});
});
const xncustomStorePageSelectRef = ref();
const customStoreSelector = (param) => {
	return supplierStoresApi.supplierStoresPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.storeName,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
nextTick(() => {
	xncustomStorePageSelectRef.value.onPage({current: 1});
});
const xncustomTagPageSelectRef = ref();
const customTagSelector = (param) => {
	return supplierCustomTagApi.supplierCustomTagPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.customTag,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
nextTick(() => {
	xncustomTagPageSelectRef.value.onPage({current: 1});
});
const customGenderOptions = tool.dictList('GENDER')
const xncustomDeliveryPageSelectRef = ref();
const customDeliverySelector = (param) => {
	return supplierDeliveryApi.supplierDeliveryPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.delivery,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
nextTick(() => {
	xncustomDeliveryPageSelectRef.value.onPage({current: 1});
});
</script>
